//
//  DDDownLoadAddress.m
//  MyClouds
//
//  Created by sarah doo on 13-8-11.
//  Copyright (c) 2013年 sarah doo. All rights reserved.
//

#import "DDDownLoadAddress.h"
#import "DDCommentDic.h"
@implementation DDDownLoadAddress


- (id)initWithData:(NSData *)data {
    self = [super initWithData:data];
    return self;
}

- (id)initWithOffset:(long)off andLength:(long)len{
    self = [super init];
    NSRange range;
    range.location = HEADER_LENGTH;
    range.length = LONG_SIZE;
    NSData* offData = [[NSData alloc]initWithBytes:&off length:sizeof(off)];
    [entity replaceBytesInRange:range withBytes:[offData bytes]];
    //[offData release];
    
    range.location = range.location + range.length;
    range.length = LONG_SIZE;
    NSData* lenData = [[NSData alloc]initWithBytes:&len length:LONG_SIZE];
    [entity replaceBytesInRange:range withBytes:[lenData bytes]];
    //[lenData release];
    
    [self setCmd:CloudCmd_DownloadAddressBook];
    [self setLength:[entity length]];
    int crc = [self crcs:entity withLength:[entity length]];
    [self setCrc:crc];
    return self;
}

-(long)getOffset{
    NSRange range;
    range.location = HEADER_LENGTH;
    range.length = LONG_SIZE;
    
    NSData* offsetData = [entity subdataWithRange:range];
    long offset = 100;
    [offsetData getBytes:&offsetData length:LONG_SIZE];
//    long offset = [self dataToLong:offsetData];
    //[offsetData release];
    return offset;
}

-(long)getLength{
    NSRange range;
    range.location = HEADER_LENGTH + LONG_SIZE;
    range.length = LONG_SIZE;

    NSData* lenData = [entity subdataWithRange:range];
//    long len = [self dataToLong:lenData];
    //[lenData release];
    long len = -1;
    [lenData getBytes:&len length:LONG_SIZE];
    return len;
}

@end
